<?php
namespace app\index\controller;

use app\common\model\CheckworkRecord;
use app\common\model\CheckworkParam;
use think\Exception;
use app\common\controller\Curls;
use app\common\controller\Wechat;
use app\common\model\CompanyStaff;
use app\common\model\CheckworkLeave;
use think\Request;

class Work extends Base
{
	/**
	 * 获取员工打卡的基本信息
	 */
	public function getWorkInfo()
	{
		//获取考勤参数
		$work_param = CheckworkParam::get(1);
		//获取今天时间戳
		$time_num = strtotime(date('Y-m-d'));
		//获取本日打卡记录
		$record = CheckworkRecord::where(['staff_id' => $this->staff_id,'create_time' => $time_num])->find();
		$record = $record ? $record->toArray() : $record;
		//今日打卡时间
		$day_record = $record ? ['state' => 1,'time' => date('H:i:s',strtotime($record['on_time'])),'off' => $record['off_time']] : ['state' => 0,'time' => ''];
		//获取本月开始时间
		$start_time = mktime(0,0,0,date('m'),1,date('Y'));
		//获取本月结束时间
		$end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
		//获取本月打卡记录
		$month_check = CheckworkRecord::where(['staff_id' => $this->staff_id,'create_time'=>['between',[$start_time,$end_time]]])->select()->toArray();
		
		$late = 0;  //初始化迟到次数
		$leave = 0; //初始化早退次数
		$not_work = 0;  //初始化未打卡次数
		$normal_work = 0;  //正常考勤
		foreach ($month_check as $rows) {
			$on_time = strtotime($rows['on_time']) ? strtotime($rows['on_time']) : 0;  //上班时间
			$off_time = strtotime($rows['off_time']) ? strtotime($rows['off_time']) : 0; // 下班时间
			$param_on_time = strtotime($work_param->on_time);
			$param_off_time = strtotime($work_param->off_time);
			if($on_time > ($param_on_time + ($work_param->late * 60)))$late ++;
			if($param_off_time > $off_time  && $off_time) $leave++;
			if($on_time < ($param_on_time + ($work_param->late * 60)) && $param_off_time >= $off_time  && $off_time)$normal_work ++;
		}
		$not_work = date('d') - count($month_check);
		$res = [
				'day_record' => $day_record,
				'late'  => $late,
				'leave' => $leave,
				'not_work' => $not_work,
				'normal_work' => $normal_work
		];
		return $this->success($res);
	}

	
	/**
	 * 员工上班打卡
	 * @throws Exception
	 */
	public function checkWork()
	{
		//31.883193,117.305839
		try {
			//获取考勤参数
			$work_param = CheckworkParam::get(1);
			//计算距离
			$distance = $this->calcDistance($work_param->latitude, $work_param->longitude, session('latitude'), session('longitude'));
			if($distance > 50)throw new Exception('不在打卡范围内');
			//获取今天时间戳
			$time_num = strtotime(date('Y-m-d'));
			//获取本日打卡记录
			$record = CheckworkRecord::where(['staff_id' => $this->staff_id,'create_time' => $time_num])->find();
			if($record)throw new Exception('非法操作');
			$time = time();  //获取当前打卡时间
			if($time <= (strtotime($work_param->on_time))) {
				$state = 1;
			}elseif($time > (strtotime($work_param->on_time)) && $time <= (strtotime($work_param->on_time) + ($work_param->late * 60))){
				$state = 2;
			}elseif($time > (strtotime($work_param->on_time) + ($work_param->late * 60))){
				$state = 3;
			}
			$data = [
					'staff_id' => $this->staff_id,
					'on_time' => time(),
					'off_time' => '',
					'state' => $state,
					'create_time' => strtotime(date('Y-m-d'))
			];
			CheckworkRecord::create($data);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		$staff_info = CompanyStaff::get($this->staff_id);
		$tmep_data = [
				"first" => [
						"value" => "上班打卡通知",
						"color" => "#000000"
				],
				"keyword1" => [
						"value" => $staff_info->name,
						"color" => "#173177"
				],
				"keyword2" => [
						"value" => $staff_info->depart_name,
						"color" => "#333333"
				],
				"keyword3" => [
						"value" => date('Y-m-d H:i:s'),
						"color" => "#333333"
				],
				"remark" => [
						"value" => "上班打卡",
						"color" => "#333333"
				]
		];
		Wechat::modelMsg($staff_info->openid, 'KpFMWlc2wU0CZi4n0u6or4zGv9EzCVaWTBRUo0l90Ho', $tmep_data);
		/* $openid = CompanyStaff::get($this->staff_id)->openid;
		Wechat::toUserMsg($openid, date('H:i:s')."上班打卡成功"); */
		return $this->success('打卡成功');
	}
	
	/**
	 * 员工下班打卡
	 * @throws Exception
	 */
	public function offCheckWork()
	{
		try {
			//获取考勤参数
			$work_param = CheckworkParam::get(1);
			//计算距离
			$distance = $this->calcDistance($work_param->latitude, $work_param->longitude, session('latitude'), session('longitude'));
			if($distance > 50)throw new Exception('不在打卡范围内');
			//获取今天时间戳
			$time_num = strtotime(date('Y-m-d'));
			//获取本日打卡记录
			$record_id = CheckworkRecord::where(['staff_id' => $this->staff_id,'create_time' => $time_num])->value('id');
			if(CheckworkRecord::where(['staff_id' => $this->staff_id,'create_time' => $time_num])->value('off_time'))
				throw new Exception('非法操作');
			if(!$record_id)throw new Exception('非法操作');
			CheckworkRecord::get($record_id)->save(['off_time' => time()]);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		} 
		$staff_info = CompanyStaff::get($this->staff_id);
		$tmep_data = [
				"first" => [
						"value" => "下班打卡通知",
						"color" => "#000000"
				],
				"keyword1" => [
						"value" => $staff_info->name,
						"color" => "#173177"
				],
				"keyword2" => [
						"value" => $staff_info->depart_name,
						"color" => "#333333"
				],
				"keyword3" => [
						"value" => date('Y-m-d H:i:s'),
						"color" => "#333333"
				],
				"remark" => [
						"value" => "下班打卡",
						"color" => "#333333"
				]
		];
		Wechat::modelMsg($staff_info->openid, 'KpFMWlc2wU0CZi4n0u6or4zGv9EzCVaWTBRUo0l90Ho', $tmep_data);
		//Wechat::toUserMsg($openid, date('H:i:s')."下班打卡成功");
		return $this->success('打卡成功');
	}
	/**
	 * 保存用户的经纬度
	 */
	public function getLocation()
	{
		$data = $this->request->param('');
		try {
			session('longitude',$data['longitude']);  //经度
			session('latitude',$data['latitude']);  //纬度
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		return $this->success('定位成功');
	}

	/**
	 * 提交请假申请
	 * @throws Exception
	 */
	public function leave()
	{
		$reason = $this->request->param('reason');
		try {
			if(!$reason)throw new Exception('请假理由不能为空');
			//验证是否多次提交请假
			//获取今日开始时间
			$start_time = mktime(0,0,0,date('m'),date('d'),date('Y'));
			//获取今日结束时间
			$end_time = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			$true = CheckworkLeave::where(['staff_id' => $this->staff_id,'create_time' => ['between',[$start_time,$end_time]]])->value('id');
			if($true)throw new Exception('请勿重复提交请假申请');
			$data = [
					'staff_id' => $this->staff_id,
					'content' => $reason,
					'create_time' => time()
			];
			CheckworkLeave::create($data);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		//公众号推送消息
		Wechat::toUserMsg($this->openid, '您的请假申请已提交请耐心等待管理员审核');
		return $this->success('请假申请成功');
	}
}